package com.epro.standard.infrastructure.root;

import java.util.Enumeration;
import java.util.Vector;

/**
 * @author admin
 * Created Date 12 �.�. 2003
 * 
 */
public class KeywordCombiCondition {
    /**
     * conditions - available classes are KeywordCondition and KeywordCombiCondition
     */
    private Vector conditions;
    private String conditionType;
    static int cnt = 0;

    public KeywordCombiCondition(String conditionType) {
        conditions = new Vector();
        this.conditionType = conditionType;
    }


    /**
     * Returns the conditionType.
     * @return String
     */
    public String getConditionType() {
        return conditionType;
    }

    /**
     * Sets the conditionType.
     * @param conditionType The conditionType to set
     */
    public void setConditionType(String conditionType) {
        this.conditionType = conditionType;
    }

    public void addSubQuery(StdKeyword keyword) {
        this.conditions.addElement(keyword);
    }

    public void addCondition(KeywordCondition keywordCondition) {
        conditions.addElement(keywordCondition);
    }

    public void addCombiCondition(KeywordCombiCondition keywordCombiCondition) {
        //System.out.println(this.conditions);		
        this.conditions.addElement(keywordCombiCondition);
        //System.out.println(this.conditions);
    }

    public void clearCondition() {
        conditions.clear();
    }

    public void clearPKCondition() {
        Vector newVector = new Vector();
        Enumeration e = conditions.elements();
        while (e.hasMoreElements()) {
            Object o = e.nextElement();
            if (o instanceof KeywordCondition) {
                KeywordCondition kc = (KeywordCondition)o;
                if (!kc.isPK())
                    newVector.addElement(kc);
            } else
                newVector.addElement(o);
        }
        conditions = newVector;
    }

    public void sort() {
        Vector temp = new Vector();
        Enumeration e = conditions.elements();
        while (e.hasMoreElements()) {
            Object o = e.nextElement();
            if ((o instanceof KeywordCombiCondition)) {
                temp.addElement(o);
            }
        }
        e = conditions.elements();
        while (e.hasMoreElements()) {
            Object o = e.nextElement();
            if ((o instanceof KeywordCondition)) {
                temp.addElement(o);
            }
        }
        e = conditions.elements();
        while (e.hasMoreElements()) {
            Object o = e.nextElement();
            if ((o instanceof StdKeyword)) {
                temp.addElement(o);
            }
        }
        conditions = temp;
    }

    public String toString() {
        return toString("");
    }

    public String toString(String tabSize) {
        sort();
        String returnString = "\n" + tabSize + "(";
        String tab = tabSize + "\t";
        int tabCount = 0;
        for (int i = 0; i < conditions.size(); i++) {
            Object con = conditions.get(i);
            if (con instanceof KeywordCombiCondition) {
                returnString += 
                        ((KeywordCombiCondition)con).toString(tab) + " " + 
                        conditionType + " ";
            } else if (con instanceof KeywordCondition) {
                returnString += 
                        ((KeywordCondition)con).toString(tab) + " " + conditionType + 
                        " ";
            } else if (con instanceof StdKeyword) {
                returnString += 
                        ((StdKeyword)con).toString(tab) + " " + conditionType + 
                        " ";
            }
        }
        returnString = 
                returnString.substring(0, returnString.length() - (conditionType.length() + 
                                                                   2));
        returnString += "\n" + tabSize + ")";
        return returnString;
    }

    public boolean contain(KeywordCondition kc) {
        boolean returnBoolean = false;
        int i = 0;
        while (i < conditions.size() && !returnBoolean) {
            Object con = conditions.get(i);
            if (con instanceof KeywordCombiCondition)
                returnBoolean = ((KeywordCombiCondition)con).contain(kc);
            else
                returnBoolean = 
                        kc.toString().equalsIgnoreCase(((KeywordCondition)con).toString());
            i++;
        }
        return returnBoolean;
    }

    public Vector getConditions() {
        return this.conditions;
    }
}

